package com.mashiro.system.service.impl;

import com.mashiro.common.utils.DateUtils;
import com.mashiro.common.utils.SecurityUtils;
import com.mashiro.system.domain.MoeFund;
import com.mashiro.system.domain.MoeMessage;
import com.mashiro.system.domain.MoeProject;
import com.mashiro.system.domain.MoeWorkOrder;
import com.mashiro.system.domain.status.MoeWorkOrderStatus;
import com.mashiro.system.domain.types.MoeMessageType;
import com.mashiro.system.domain.types.MoeWorkOrderType;
import com.mashiro.system.mapper.MoeFundMapper;
import com.mashiro.system.mapper.MoeMessageMapper;
import com.mashiro.system.mapper.MoeProjectMapper;
import com.mashiro.system.mapper.MoeWorkOrderMapper;
import com.mashiro.system.service.IMoeFundService;
import com.mashiro.system.service.IMoeWorkOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 工单管理Service业务层处理
 *
 * @author mashiro
 * @date 2023-04-14
 */
@Service
public class MoeWorkOrderServiceImpl implements IMoeWorkOrderService {
    @Resource
    private MoeWorkOrderMapper moeWorkOrderMapper;
    @Resource
    private MoeMessageMapper moeMessageMapper;
    @Resource
    private MoeProjectMapper moeProjectMapper;
    @Resource
    private MoeFundMapper moeFundMapper;

    @Resource
    private IMoeFundService moeFundService;

    /**
     * 查询工单管理
     *
     * @param id 工单管理主键
     * @return 工单管理
     */
    @Override
    public MoeWorkOrder selectMoeWorkOrderById(Long id) {
        return moeWorkOrderMapper.selectMoeWorkOrderById(id);
    }

    /**
     * 查询工单管理列表
     *
     * @param moeWorkOrder 工单管理
     * @return 工单管理
     */
    @Override
    public List<MoeWorkOrder> selectMoeWorkOrderList(MoeWorkOrder moeWorkOrder) {
        return moeWorkOrderMapper.selectMoeWorkOrderList(moeWorkOrder);
    }

    /**
     * 新增工单管理
     *
     * @param moeWorkOrder 工单管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMoeWorkOrder(MoeWorkOrder moeWorkOrder) {
        moeWorkOrder.setCreateTime(DateUtils.getNowDate());
        Long userId = SecurityUtils.getUserId();
        MoeMessage moeMessage = new MoeMessage();
        moeWorkOrder.setSenderId(userId);
        moeMessage.setSenderId(userId);
        if (MoeWorkOrderType.PROJECT_DELAY.getValue().equals(moeWorkOrder.getType())){
            moeMessage.setTitle("工程延期工单");
        } else if (MoeWorkOrderType.FUND_EXPANSION.getValue().equals(moeWorkOrder.getType())) {
            moeMessage.setTitle("资金扩充工单");
        }
        moeMessage.setType(MoeMessageType.PROJECT_CHANGE.getValue());
        moeMessage.setContent("您有新的项目工单未处理\n" + "项目ID: " + moeWorkOrder.getProjectId().toString() + "\n请快速处理");
        moeMessage.setReceiverId(moeWorkOrder.getReceiverId());// 向所有项目管理员发送工单消息
        moeMessageMapper.insertMoeMessage(moeMessage);
        return moeWorkOrderMapper.insertMoeWorkOrder(moeWorkOrder);
    }

    /**
     * 修改工单管理
     *
     * @param moeWorkOrder 工单管理
     * @return 结果
     */
    @Override
    public int updateMoeWorkOrder(MoeWorkOrder moeWorkOrder) {
        MoeMessage moeMessage = new MoeMessage();
        Date date = DateUtils.getNowDate();
        moeWorkOrder.setUpdateTime(date);
        moeMessage.setCreateTime(date);
        moeMessage.setTitle("项目ID: [" + moeWorkOrder.getProjectId() + "]工单处理结果");
        if (MoeWorkOrderStatus.APPROVED.getValue().equals(moeWorkOrder.getStatus())){
            if (MoeWorkOrderType.PROJECT_DELAY.getValue().equals(moeWorkOrder.getType())){
                moeMessage.setContent("工单通过，允许项目延期");
                MoeProject moeProject = moeProjectMapper.selectMoeProjectById(moeWorkOrder.getProjectId());
                moeProject.setEndTime(moeWorkOrder.getProjectNewEndTime());
                moeProjectMapper.updateMoeProject(moeProject);
            }else if (MoeWorkOrderType.FUND_EXPANSION.getValue().equals(moeWorkOrder.getType())){
                moeMessage.setContent("工单通过，允许资金扩充");
                MoeFund projectFund = moeFundMapper.selectMoeFundByProjectId(moeWorkOrder.getProjectId());
                // 工单新增的资金，一律增加到公司资金中
                projectFund.setCompanyFunding(projectFund.getCompanyFunding() + moeWorkOrder.getIncreaseFunding());
                moeFundService.updateMoeFund(projectFund);
            }
        }else if (MoeWorkOrderStatus.REJECTED.getValue().equals(moeWorkOrder.getStatus())){
            moeMessage.setContent("工单驳回，请从自身找问题!");
        }
        return moeWorkOrderMapper.updateMoeWorkOrder(moeWorkOrder);
    }

    /**
     * 批量删除工单管理
     *
     * @param ids 需要删除的工单管理主键
     * @return 结果
     */
    @Override
    public int deleteMoeWorkOrderByIds(Long[] ids) {
        return moeWorkOrderMapper.deleteMoeWorkOrderByIds(ids);
    }

    /**
     * 删除工单管理信息
     *
     * @param id 工单管理主键
     * @return 结果
     */
    @Override
    public int deleteMoeWorkOrderById(Long id) {
        return moeWorkOrderMapper.deleteMoeWorkOrderById(id);
    }
}
